package com.lebang.controller.sys;

import com.lebang.aop.ConfigRecord;
import com.lebang.common.Login;
import com.lebang.component.RedisClient;
import com.lebang.domain.TaskRouse;
import com.lebang.entity.sys.Module;
import com.lebang.example.TaskRouseExample;
import com.lebang.model.Whether;
import com.lebang.model.enums.buss.RewardTypeUscan;
import com.lebang.service.AppEarnRouseSortService;
import com.lebang.service.DownAppService;
import com.lebang.service.NewRouseService;
import com.lebang.service.UserService;
import com.lebangweb.common.TextUtil;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.lebang.service.DownAppService.TASK_ROUSE_POOLS;

/**
 * 每日必赚管理后台
 *
 * @Author: hwq
 * @Date: 2020/1/2 16:43
 * @Version 1.0
 */
@Controller
@RequestMapping("/sys/newrouse")
public class NewRouseController {
    @Autowired
    private NewRouseService baseService;
    @Autowired
    private DownAppService downAppService;
    @Autowired
    private AppEarnRouseSortService appEarnRouseSortService;
    @Autowired
    private UserService userService;
    @Autowired
    private NewRouseService newRouseService;
    @Value("#{'${check.sdkList}'.split(',')}")
    private List<String> sdkList;

    @Login
    @GetMapping("/list")
    public String list(Model model, HttpSession session) {
        List<TaskRouse> rouses = baseService.selectAll();
        rouses = newRouseService.checkRoot(rouses,session);
        model.addAttribute("item", rouses);
        return "/newrouse/list";
    }


    @Login
    @PostMapping("/data")
    @ResponseBody
    public Map<String, Object> datalist(HttpSession session
            , @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")LocalDateTime start
            , @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")LocalDateTime end
            , @RequestParam(required = false) String pkgName
            , @RequestParam(required = false) String appName
            ,Long page, Integer limit){
        TaskRouseExample example = new TaskRouseExample();
        example.setOffset((page - 1) * limit);
        example.setLimit(limit);
        TaskRouseExample.Criteria criteria = example.createCriteria();
        if(!TextUtil.isEmpty(start)&&!TextUtil.isEmpty(end)){
            criteria.andCreateTimeBetween(start,end);
        }
        if(newRouseService.isRoot(session)){
            criteria.andSdkKeyNotIn(sdkList);
        }
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("data", baseService.selectByExample(example));
        returnMap.put("count", baseService.countByExample(example));
        returnMap.put("code", 0);
        return returnMap;


    }


    @Login
    @GetMapping("/add")
    public String add(Long id, Model model) {
        TaskRouse vo;
        if (id != null) {
            vo = baseService.selectById(id);
        } else {
            vo = new TaskRouse();
            vo.setRewardType(RewardTypeUscan.Point.getIndex());
            vo.setEnable(Whether.NO.getDbIndex());
            vo.setHasClick(Whether.YES.getDbIndex());
            vo.setOpenReturn(Whether.YES.getDbIndex());
        }
        model.addAttribute("item", vo);
        return "/newrouse/add";
    }

    @Autowired
    private RedisClient redisClient;

    @Login
    @PostMapping("/edit")
    @ResponseBody
    @ConfigRecord
    public JsonData edit(TaskRouse sort) {
        if (sort.getId() != null) {
            sort.setModifyTime(LocalDateTime.now());
            TaskRouse po = baseService.selectById(sort.getId());
            if (!po.getHasClick().equals(sort.getHasClick())) {
                redisClient.del(downAppService.getClickCacheKey());
            }
            baseService.updateById(sort);
        } else {
            sort.setCreateTime(LocalDateTime.now());
            sort.setHasDeleted(Whether.NO.getDbIndex());
            baseService.insertModel(sort);
        }
        redisClient.del(TASK_ROUSE_POOLS);
        baseService.delCacheListByKey();
        baseService.selectAll();
        baseService.initInfo();
        return Callback.success("success", "修改成功");
    }

    @PostMapping("/enable")
    @ResponseBody
    @Login
    @ConfigRecord
    public JsonData enable(@RequestBody TaskRouse rouse) {
        baseService.updateById(rouse);
        redisClient.del(TASK_ROUSE_POOLS);
        baseService.delCacheListByKey();
        baseService.initInfo();
        return Callback.success("success", "修改成功");
    }

    }
